<?php
/**
 * Created by PhpStorm.
 * User: t
 * Date: 2024/6/24
 * Time: 9:15
 */
namespace app\common\model;

use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use app\services\wechat\Miniprogram;
use app\services\wechat\Offiaccount;

use think\Model;
class MessageTemplate extends Model
{
    protected $name = "message_template";
    public function sendMessage($type,$data)
    {
        $result = ['code' => 1];
        try {
            $userModel = new User();
            foreach ($data as $value){
                $userInfo = $userModel->where(['id' => $value['uid']])->find();
                if ($type == 1){
                    if ($userInfo['user_gzh_id'] >0){
                        $userGzhModel = new UserGzh();
                        $userGzhInfo= $userGzhModel->where(['id' => $userInfo['user_gzh_id']])->find();
                        $messageInfo = $this->where(['title' => $value['title']])->where(['type' => 1])->find();
                        if (empty($messageInfo)) {
                            return ['code' => 1];
                        }

                        if ($messageInfo['status'] == 1 && $messageInfo['tid']) {
                            $Offiaccount= new Offiaccount();
                            $result = $Offiaccount->Message($userGzhInfo['openid'], $value['data'], $messageInfo['tid']);
                        }
                    }
                }else{
                    if ($userInfo['user_xcx_id'] >0){
                        $userXcxModel = new UserXcx();
                        $userXcxInfo = $userXcxModel->where(['id' => $userInfo['user_xcx_id']])->find();
                        $messageInfo = $this->where(['title' =>  $value['title']])->where(['type' => 3])->find();
                        if (empty($messageInfo)) {
                            return ['code' => 1];
                        }
                        if ($messageInfo['status'] == 1 && $messageInfo['tid']) {
                            $Miniprogram = new Miniprogram();
                            $result = $Miniprogram->Message($userXcxInfo['openid'], $value['data'], $messageInfo['tid']);
                        }
                    }
                }
            }
        } catch (\Exception $exception) {

            return false;
        }
        return $result;
    }

    public function sendAliMessage($uid, $title, $field)
    {
        $userModel = new User();
        $userInfo = $userModel->where(['id' => $uid])->find();
        if ($userInfo['phone']){
            try {
                $messageInfo = $this->where(['title' => $title])->where(['type' => 2])->find();
                if (empty($messageInfo)) {
                    return false;
                }
                if ($messageInfo['status'] == 1) {
                    $accessKeyId = Config::getConfigValue('ali_duanxin_access_key_secret');//为企阿里云账号秘钥ID
                    $accessKeySecret = Config::getConfigValue('ali_duanxin_access_key_id');;//秘钥

                    $config = new Config(['accessKeyId' => $accessKeyId, 'accessKeySecret' => $accessKeySecret]);
                    $config->endpoint = "dysmsapi.aliyuncs.com";
                    $client = new \AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi($config);
                    $sendSmsRequest = new SendSmsRequest([
                        "phoneNumbers" => $userInfo['phone'],
                        "signName" => $messageInfo['template_name'],    //    签名
                        "templateCode" => $messageInfo['tid'],    //    模板
                        "templateParam" => json_encode($field)
                    ]);
                    $runtime = new RuntimeOptions([]);
                    $res = $client->sendSmsWithOptions($sendSmsRequest, $runtime);
                    if ($res->body->code == 'OK') {
                        return true;
                    }
                    return false;

                } else {
                    return false;
                }
            } catch (\Exception $exception) {
                return false;
            }
        }else{
            return false;
    }



    }
}